package com.cqs.learning.concurrent;

import com.cqs.learning.util.TimeUtil;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

/**
 * 1.线程状态与锁的关系
 * 2. 线程状态与中断的关系
 * 3. 中断与锁的关系
 *
 * @author lixiaowen
 * @create 2020-02-01
 */
public class ThreadStatus {

    private static ReentrantLock lock = new ReentrantLock();


    /**
     * 1.中断不一定能被响应
     */
    public static void interruptNotResponse() {
        Thread t = new Thread() {
            @Override
            public void run() {
                int c = 0;
                while (true) {
                    //线程进入RUNNABLE状态,不会响应中断
                    Thread.yield();
                    if (++c % 500000 == 0) {
                        System.out.println(Thread.currentThread() + "\t" + TimeUtil.curTime());
                    }
                }
            }
        };
        t.start();
        Thread.yield();
        //只是
        t.interrupt();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //stop 可以终止线程
        //t.stop();
        System.out.println(t.getState());
    }

    /**
     *
     */
    public static void interruptRelease(){
        Consumer<Integer> consumer = new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                synchronized (Thread.class){

                    Thread.yield();
                }
            }
        };

        Thread thread = new Thread(""){

        };
    }


    /**
     * 1. 阻塞状态下响应中断
     * 2. 休眠状态下可以响应中断
     * 3. 所有状态下均可以被stop终止
     */
    public static void blockedInterrupt(){
        lock.lock();
        Thread t = new Thread(){
            @Override
            public void run() {
                System.out.println(getName() + "\t" + " 尝试获取锁");
                //阻塞线程
                try {
                    lock.tryLock(5, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                try {
//                      //休眠状态可以响应中断
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                System.out.println(getName() + "\t" + "获取锁成功");
            }
        };
        t.start();
        //中断: 失败 线程t不能运行,自然不能主动响应失败
        t.interrupt();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getState() + "\t 是否被中断: " + t.isInterrupted());//WAITING	 是否被中断: false
        t.stop(); //终止线程
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getState());//TERMINATED
    }


    /**
     * 1. 线程状态与锁的关系
     *
     * 2.
     */

    /**
     * interruptNotResponse
     */
    public static void interruptAndResource() {
        class Task extends Thread {
            public Task(String name) {
                super(name);
            }

            @Override
            public void run() {
                synchronized (Task.class) {
                    while (true) {
                        lock.lock();
                        if (this.isInterrupted()) {
                            System.out.println(Thread.currentThread().getName() + "响应中断");
                            break;
                        }
                        System.out.println(Thread.currentThread() + "\t" + TimeUtil.curTime());
//                        try {
//                            Thread.sleep(700);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                            break;
//                        } finally {
//
//                        }
                        Thread.yield();
                        lock.unlock();
                    }
                }
            }

            @Override
            protected void finalize() throws Throwable {
                System.out.println(this.getName() + "结束了");
            }
        }
        Task t1 = new Task("线程-1");
        Task t2 = new Task("线程-2");
        t2.start();
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.interrupt();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }


    public static void main(String[] args) {
//        interruptNotResponse();
        blockedInterrupt();
    }
}
